<?php
class Contact_m extends CI_Model{
	function __construct(){
		parent :: __construct();
		$this->load->database();
	}
	
	//--list all contact--//
	function list_contact($per_page, $off_set){
		$query=
			$this->db->select()
					 ->from('contact')
					 ->limit($per_page, $off_set)
					 ->get();
		if($query->num_rows()>0){
			return $query->result_array();
		}
	}
	
	//--count all contact--//
	function count_contact(){
		$query = 
				$this->db->select()
						 ->from('contact')
						 ->get();
		if($query->num_rows()>=0){
			return $query->num_rows();
		}else{
			return array();
		}
	}

	//--count email--//
	function get_email($email, $inputemail, $table){
		$query = $this->db->where($email, $inputemail)->get($table);
		return $query;
	}
	
	//--count email edit--//
	function count_email_edit($email, $contact_id){
		$this->db->where('email', $email);
		$this->db->where_not_in(array("id" => $contact_id));
		$this->db->from('contact');
		$cnt = $this->db->count_all_results();
		return $cnt;
	}
	
	//--add contact--//
	function add_contact($addition_data = null){
		$data = array();
		$collums = array('first_name', 'last_name', 'position', 'email');
		if(!empty($collums)){
			foreach($collums as $cols){
				if(is_array($addition_data) && isset($addition_data[$cols])){
					$data[$cols] = $addition_data[$cols];
				}
				elseif($this->input->post()){
					$data[$cols] = $this->input->post($cols);
				}
			}
		}
		
		$query = $this->db->insert("contact", $data);	
		if($query>0){
			return $query;
		}else{
			return FALSE;
		}
	}
	
	//--get contact details--//
	function get_contact_details($contact_id){
		$query = 
			 	$this->db->select()
			 			 ->from("contact")
					     ->where("id", $contact_id)
					     ->limit(1)
					     ->get();
		if($query -> num_rows() >0){
			return $query->row();
		}else{
			return FALSE;
		}
	}			

	//--edit contact--//
	function edit_contact($contact_id, $addition_data = null){
		$data = array();
		$collums = array('first_name', 'last_name', 'position', 'email');
		if(!empty($collums)){
			foreach($collums as $cols){
				if(is_array($addition_data) && isset($addition_data[$cols])){
					$data[$cols] = $addition_data[$cols];
				}
				elseif($this->input->post()){
					$data[$cols] = $this->input->post($cols);
				}
			}
		}
		
		$query = $this->db->update("contact", $data, array("id" => $contact_id));	
		if($query>0){
			return $query;
		}else{
			return FALSE;
		}
	}
	
	//--delete contact--//
	function delete_contact($id){
		$query = $this->db->delete('contact', array('id' => $id));
		if($query){
			return $query;
		} else{
			return FALSE;
		}
		
	}
	
	//--delete multi contacts--//
	function delete_multi_contacts($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('contact');
        
        return $query;

    }
    
    //--list site information--//
    function list_site_information(){
    	$query = 
    			$this->db->select()
    					 ->from('site_information')
    					 ->get();
    	if($query->num_rows()>0){
    		return $query->result_array();
    	}else{
    		return array();
    	}
    }
    
    //--get site id--//
    function get_site_id(){
    	$query = 
	    	$this->db->select()
	    			 ->from('site_information')
	    			 ->order_by('id', "ASC")
	    			 ->limit(1)
	    			 ->get();
    	if($query->num_rows()>0){
    		return $query->row();
    	}else{
    		return FALSE;
    	}
    }
    
    //--get site by id--//
	function get_site_by_id($id){
    	$query = 
	    	$this->db->select()
	    			 ->from('site_information')
	    			 ->where('id', $id)
	    			 ->order_by('id', "ASC")
	    			 ->limit(1)
	    			 ->get();
    	if($query->num_rows()>0){
    		return $query->row();
    	}else{
    		return FALSE;
    	}
    }
    
    //--add site information--//
	function add_site_information($addition_data = null){
		$data = array();
		$collums = array('website_address', 'email');
		if(!empty($collums)){
			foreach($collums as $cols){
				if(is_array($addition_data) && isset($addition_data[$cols])){
					$data[$cols] = $addition_data[$cols];
				}
				elseif($this->input->post()){
					$data[$cols] = $this->input->post($cols);
				}
			}
		}
		
		$query = $this->db->insert("site_information", $data);	
		if($query>0){
			return $query;
		}else{
			return FALSE;
		}
	}
    //--update site information--//
	function update_site_information($id, $addition_data = null){
		$data = array();
		$collums = array('website_address', 'email');
		if(!empty($collums)){
			foreach($collums as $cols){
				if(is_array($addition_data) && isset($addition_data[$cols])){
					$data[$cols] = $addition_data[$cols];
				}
				elseif($this->input->post()){
					$data[$cols] = $this->input->post($cols);
				}
			}
		}
		
		$query = $this->db->update("site_information", $data, array('id'=>$id));	
		if($query>0){
			return $query;
		}else{
			return FALSE;
		}
	}
	
	//--delete site information--//
	function delete_site_information($id){
		$query = $this->db->delete('site_information', array('id' => $id));
		if($query){
			return $query;
		} else{
			return FALSE;
		}
		
	}
}

